package io.unicorn.modules.watersub.controller;

import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.water.entity.ProDataEntity;
import io.unicorn.modules.watersub.service.ProDataSubService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2022-01-20 20:04:07
 */
@RestController
@RequestMapping("watersub/prodatahistorysub")
public class ProDataSubController {
    @Autowired
    private ProDataSubService proDataService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("watersub:prodataanalysesub:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = proDataService.getDataByProcedure(params);//proDataService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/listNb")
    @RequiresPermissions("watersub:prodataanalysesub:list")
    public R listnb(@RequestParam Map<String, Object> params){

        PageUtils page = proDataService.getDataByProcedureNb(params);//proDataService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{clientId}")
    @RequiresPermissions("watersub:prodataanalysesub:info")
    public R info(@PathVariable("clientId") String clientId){
        //Map<String, Object> params = new HashMap<String, Object>();
        //params.put("clientId",clientId);


        //PageUtils page = proDataService.getDataByProcedure(params);//
        ProDataEntity proData = proDataService.getById(clientId);
        //if (page.getList().size() > 0) proData = (ProDataEntity) page.getList().get(0);

        return R.ok().put("proData", proData);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("watersub:prodataanalysesub:save")
    public R save(@RequestBody ProDataEntity proData){
        proDataService.save(proData);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("watersub:prodataanalysesub:update")
    public R update(@RequestBody ProDataEntity proData){
        ValidatorUtils.validateEntity(proData);
        proDataService.updateById(proData);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("watersub:prodataanalysesub:delete")
    public R delete(@RequestBody String[] clientIds){
        proDataService.removeByIds(Arrays.asList(clientIds));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("watersub:prodataanalysesub:list")
    public void export(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));

        PageUtils page = proDataService.getDataByProcedure(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.ProDataEntity",
                listEntity);
    }

    @RequestMapping("/exportnb")
    @RequiresPermissions("watersub:prodataanalysesub:list")
    public void exportnb(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));

        PageUtils page = proDataService.getDataByProcedureNb(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.ProDataEntity",
                listEntity);
    }

}
